//
// Created by Tan Ke on 2023/11/25.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct ListNode
{
    int val;
    struct ListNode* next;
};

struct ListNodeHead
{
    int len;
    struct ListNode* next;
};

struct ListNode* removeNthFromEnd(struct ListNode* head, int n)
{
    int len = 0;
    int index = 0;

    struct ListNodeHead listHead;
    listHead.len = 0;
    listHead.next = head;

    struct ListNode* item = listHead.next;
    while (item != NULL) {
        item = item->next;
        listHead.len += 1;
    }

    index = listHead.len - n;
    item = listHead.next;
    struct ListNode* prev = listHead.next;

    if (index == 0) { // 如果是删除头节点
        return listHead.next->next;
    }

    for (int i = 0; i < index; i ++) {
        prev = item;
        item = item->next;
    }

    if (item == NULL) {
        prev->next = NULL;
    } else {
        prev->next = item->next;
    }

    return listHead.next;
}

int main(void)
{
    return 0;
}